/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ec.socialfun.util;


import com.ec.socialfun.excepciones.EncriptacionException;
import java.security.MessageDigest;

/**
 *
 * @author hp
 */
public class EncriptacionUtil {

    private static String MODO_ENCRIPTACION = "MD5";
    
    public static String encriptar(Object codigo) throws EncriptacionException {
        try {
            
            String contenido = String.valueOf(codigo);
                      
            MessageDigest md = MessageDigest.getInstance(MODO_ENCRIPTACION);
            byte[] b = md.digest(contenido.getBytes());

            int size = b.length;
            StringBuffer h = new StringBuffer(size);
            for (int i = 0; i < size; i++) {
                int u = b[i] & 255;
                if (u < 16) {
                    h.append("0" + Integer.toHexString(u));
                } else {
                    h.append(Integer.toHexString(u));
                }
            }
            return h.toString();

        } catch (Exception e) {
            throw new EncriptacionException("Error al encriptar codigo " + e);
        }
    }
    
    public static String crearKeyUsuario(Object codigo) throws EncriptacionException {
        try {
            
           return encriptar(codigo);

        } catch (Exception e) {
            throw new EncriptacionException("Error al encriptar codigo " + e);
        }
    }
    
}
